package com.caigou.modules.home.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.Banner
import com.caigou.comm.base.bean.UserBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.home.bean.PrizeTimeBean
import com.caigou.modules.home.bean.PrizeBean
import com.caigou.modules.home.repository.HomeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/16
 */
@HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepository) : ViewModel() {

    val bannerLiveData = MutableLiveData<Resource<MutableList<Banner>>>()
    val prizeResultLiveData = MutableLiveData<Resource<PrizeBean>>()
    val historyPrizeListLiveData = MutableLiveData<Resource<MutableList<PrizeBean>>>()
    val openPrizeTimeLiveData = MutableLiveData<Resource<MutableList<PrizeTimeBean>>>()
    val miStatusLiveData = MutableLiveData<Resource<String>>()

    fun getBanners() {
        viewModelScope.launch {
            repository.getBanners()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<Banner>>(-1, message)
                    Logger.e(it.message)
                }
                .collectLatest {
                    bannerLiveData.postValue(it)
                }
        }
    }

    fun getPrizeResult(type: Int,userId: String) {
        prizeResultLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPrizeResult(type,userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PrizeBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    prizeResultLiveData.value = it
                }
        }
    }

    fun getHistoryPrizeList(type: Int, year: Int) {
        historyPrizeListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getHistoryPrizeList(type, year)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<PrizeBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    historyPrizeListLiveData.value = it
                }
        }
    }

    fun getOpenPrizeTime() {
        openPrizeTimeLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getOpenPrizeTime()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<PrizeBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    openPrizeTimeLiveData.value = it
                }
        }
    }

    fun setMiStatus(period: Int, userId: String, type:Int,status: Int) {
        miStatusLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setMiStatus(period, userId, type,status)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserBean>(-1, message)
                    Logger.e("catch ${it.stackTrace}")
                }
                .collectLatest {
                    miStatusLiveData.value = it
                }
        }
    }
}